<template>
  <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogVisible" width="360px">
    <el-form ref="formItem" :model="formItem" label-width="80px" size="small" :rules="rules">
      <el-row>
        <el-col :span="24">
          <el-form-item label="客户" prop="custId">
            <el-input v-model="cust.name" disabled style="width:215px" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="formItem.projectName" style="width:215px" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="业务员" prop="userId">
            <el-select v-model="formItem.userId" clearable filterable placeholder="请选择">
              <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="结束时间" prop="validDate">
            <el-date-picker v-model="formItem.validDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="" prop="ifSingle">
            <el-checkbox v-model="formItem.ifSingle">单独结算</el-checkbox>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit('formItem')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getCustByCustId } from '@/api/cust'
import { getUserList } from '@/api/user'
import { addProject, updateProject } from '@/api/project'
export default {
  data () {
    return {
      isEdit: false,
      dialogVisible: false,
      title: '',
      userList: [],
      cust: {},
      formItem: {
        custId: '',
        id: '',
        projectName: '',
        ifSingle: true,
        userId: '',
        validDate: ''
      },
      rules: {
        projectName: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
        userId: [{ required: true, message: '请选择业务员', trigger: 'blur' }],
      }
    }
  },
  methods: {
    init (row, defaultValue) {
      if (row) {
        this.title = '编辑'
        this.formItem = _.clone(row)
        this.isEdit = true
        delete this.formItem.updateTime
      } else {
        this.title = '新增'
        this.formItem = Object.assign(this.formItem, defaultValue)
        this.formItem.userId = this.$store.getters.getUser.userId
        if (this.formItem.custId) {
          this.queryCustByCustId(this.formItem.custId)
        }
        this.isEdit = false
      }
      this.dialogVisible = true
      this.queryUserList()
    },
    async queryCustByCustId (custId) {
      const res = await getCustByCustId(custId)
      if (res.code === 200) {
        this.cust = { ...res.data }
      } else {
        this.$message.error(res.msg)
        this.cust = {}
      }
    },
    async queryUserList () {
      const res = await getUserList()
      if (res.code === 200) {
        this.userList = res.data
      } else {
        this.userList = []
        this.$message.error(res.msg)
      }
    },
    handleSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = this.isEdit ? await updateProject(this.formItem) : await addProject(this.formItem)
          if (res.code === 200) {
            this.$emit('ok')
            this.dialogVisible = false
          } else {
            this.$message.error(res.msg)
          }
        }
      })
    }
  }
}
</script>

<style>
</style>